import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import SpecCode from '#tp/specCode'
// ===================================================================== global declare
const { $fn, $Lazy, $http } = window
// ===================================================================== private component
const Box 				= $Lazy.load(()=>import('#tp/box/box'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
const $time 			= $Lazy.file(()=>import('@com/utils/time'))
const EntryProject		= $Lazy.state(()=>import('./entry-project'))
const ImgEdit 			= $Lazy.state(()=>import('../../basicInfo-entry/cpt/img-edit'))
// ===================================================================== Data
const AuditStatus = [{ name:'待审核', value:'0' },{ name:'已审核', value:'1' },{ name:'待保存', value:'-2' },{ name:'待录入', value:'-3' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 搜索表单列表
		forms: [
			{ label: '条码号',		name: 'spec_code' },
			{ label: '录入时间',		names: ['start_at','end_at'], 	type:'date-range', name: 'jz-time' },
			{ label: '医院名称',		name: 'hosp_id',				type: 'select', 	loading:true },
		],
		selectedKeys: [],
		specData:[]
	}
	model = {}
	componentDidMount(){
		this.getSelect()
		this.onTime()	
	}
	onTime = () => {
		$time.then(f=>{
			const arr = f.duration['2-0-24']()
			this.resetModel = { start_at: arr[0], end_at: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			this.fetch()
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 医院
			$fn.setRow( forms, 'hosp_id',  await f.getCache({ api:'hospital' }))
			//
			this.setState({ forms })
		})
	}
	// paging
	fetch = ()  => $fn.fetch.call(this,'specimen-kind-record/diffList',{isIndex:true})
	// 表单
	cols = [
		{ title: '条码号', 		dataIndex: 'spec_code',  			...SpecCode(),  			sorter:true },
		{ title: '医院名称', 	dataIndex: 'hosp_name', 			 width:200 },
		{ title: '标本箱号', 	dataIndex: 'box_num',				...$fn.table.exp_num,	 },
		{ title: '采集时间', 	dataIndex: 'coll_time',  			...$fn.table.time,  		sorter:true },
		{ title: '采集类型', 	dataIndex: 'collect_type_name',  	width:100  },			 /* 添加采集类型 2021-8-11 --马婷 */
		{ title: '标本类型', 	dataIndex: 'spec_type_name', 		 width:150, },
		{ title: '项目状态', 	dataIndex: 'enabled', 			 	width:100,					render: text => $fn.getState(AuditStatus,text) },
		{ title: '项目列表', 	dataIndex: 'kind_str', 				 width:250 },
		{ title: '录入时间', 	dataIndex: 'arrival_time',  		...$fn.table.time,			sorter:true, },
		{ title: '第三方来源', 	dataIndex: 'third_source_type_name', width:150 },
		{ title: '第三方条码', 	dataIndex: 'third_spec_code', 		 width:150 },
		{ title: '第三方医院', 	dataIndex: 'third_hosp_name', 		 width:150 },
	]
	
	controls = () => {
		return [
			Controls.back.call(this,{refresh:true} ),   														// 返回
		]
		
	}
	// 表格的点击事件
	onRow = (spec_code)=> {
		$fn.disabledRows(this.entryProjectRef1.state.formData, 'project_id', false)
		$fn.disabledRows(this.entryProjectRef2.state.formData, 'project_id', false)
		$http.submit(null, 'specimen/getspeccodeInfo', { param:{spec_code} } ).then(data=>{
			if($fn.hasObject(data)){
				this.setState({specData:data})
				const { hosp_name, spec_code} = data
				this.entryProjectRef1.formRef.setValue({ hosp_name, spec_code })
				this.entryProjectRef1.fetch()
				this.entryProjectRef2.formRef.setValue({ hosp_name, spec_code })
				this.entryProjectRef2.fetch()
			}
		})
	}

	clear = () => {
		this.entryProjectRef1.formRef.reset()
		this.entryProjectRef1.clear()
		this.entryProjectRef2.formRef.reset()
		this.entryProjectRef2.clear()
		this.setState({SpecCode:{},selectedKeys:[]})
	}
	
	onRefresh = ()=> {
		this.setState({selectedKeys:[],specData:{}})
		$fn.disabledRows(this.entryProjectRef1.state.formData, 'project_id', true)
		$fn.disabledRows(this.entryProjectRef2.state.formData, 'project_id', true)
		this.fetch().then(data=>{
			const rows = this.state.data[this.index]
			if(rows){
				this.setState({selectedKeys:[rows.index]})
				this.onRow(rows.spec_code)
			}
		})
	}
	refreshProject = () => {
		this.entryProjectRef1.fetch()
		this.entryProjectRef2.fetch()
	}
	
	// 双录入调整，分别显示第一次录入，第二次录入 2022/6/17  --马婷
	render(){
		const { specData } = this.state
		return (
			<>
				{/* 搜索 */}
				{ SearchForm.call(this, {timeStr:'2-0-24'})}
				{/* 表格 */}
				<div className='fx h'>
					<div className='h ex'>
						<Box title="项目清单" className='ex h' flex contentClassName='rel fv pt10'>
							{ Table.call(this, { type:1, isIndex:true, sort:true,
								onRow: ({spec_code},index) => {
									this.index = index
									this.onRow(spec_code)
								},
							} )}
						</Box>
					</div>
					<div className='pr10' style={{ width:500 }}>
						<EntryProject title='第一次录入' 
							status = { 1 }
							onRef={ref =>this.entryProjectRef1=ref}
							specData = {specData}
							onRefresh = { this.onRefresh }
							refreshProject = {this.refreshProject}
							clear = {this.clear}
						/>
					</div>
					<div style={{ width:500 }}>
						<EntryProject title='第二次录入'
							status = { 2 }
							onRef={ref =>this.entryProjectRef2=ref} 
							specData = {specData}
							onRefresh = {this.onRefresh}
							refreshProject = {this.refreshProject}
							clear = {this.clear}
						/>
					</div>
				</div>
				<div style={{minHeight: 300, maxHeight: 300}}>
					<ImgEdit noBg src={specData?.image} doubleClick/>
				</div>
			</>
		)
	}
}

export default Index